home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / tool_inc.zip / VARSTR.INC < prev    next >
Text File  |  1989-06-02  |  2KB  |  64 lines

  1.  
  2. (*
  3.  * Copyright 1987, 1989 Samuel H. Smith;  All rights reserved
  4.  *
  5.  * This is a component of the ProDoor System.
  6.  * Do not distribute modified versions without my permission.
  7.  * Do not remove or alter this notice or any other copyright notice.
  8.  * If you use this in your own program you must distribute source code.
  9.  * Do not use any of this in a commercial product.
  10.  *
  11.  *)
  12.  
  13. (*
  14.  * varstr.inc - Library to manipulate variable-allocation strings. (3-1-89)
  15.  *
  16.  *)
  17.  
  18. procedure releasestr( var str:  varstring);
  19.    (* release the memory used by a varstring variable.  variable MUST
  20.       be pre-allocated or the program may crash!!! *)
  21. var
  22.    olen: integer;
  23. begin
  24.    if str <> nil then
  25.    begin
  26.       olen := ord(str^[length(str^)+1]);
  27.       if length(str^) <> olen then
  28.       begin
  29. (**
  30. writeln(^G'release: [',str^,'] ',olen,^G^G^G);
  31. **)
  32.          olen := olen div 0; {signal runtime error}
  33.       end;
  34.       
  35.       freemem(str,length(str^)+2);
  36.       str := nil;
  37.    end;
  38. end;
  39.  
  40.  
  41. procedure savestr( var tostr: varstring;
  42.                    from:      longstring);
  43.    (* save a regular string in a varstring; new allocation of varstring *)
  44. begin
  45.  
  46. (*******
  47. saved := saved+length(from)+2;
  48. writeln('avail: ',maxavail:5,
  49.         ' saved: ',saved:5,
  50.         ' ovhd: ',saved+maxavail:5,
  51.         ' save: ',from);
  52. *********)
  53.  
  54.    releasestr(tostr);
  55.    if maxavail < length(from)+100 then
  56.       {$i-} writeln('out of heap, savestr: [',from,']'); {$i+}
  57.  
  58.    getmem(tostr, length(from)+2);
  59.    tostr^ := from;
  60.    tostr^[length(from)+1] := chr(length(from));
  61. end;
  62.  
  63.  
  64.